<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/29
 * Time: 10:42
 */
namespace app\common\controller;

use think\Controller;
use think\Db;


class GoldBean extends Controller{


    /**
     * 订单完成金豆分配
     */
    public function beanDistribution($uid=NULL,$order_id=0,$level = 2){
        //$uid = 72;
        if(!$uid){
            renderJson("信息有误",0);
        }

        $reward_money = 0.00;//奖励的总金额

        //获取配置的参数
        $sys = new \app\admin\model\System();

        $first_reward =$sys->getValue("order_paike_first_reward");//第一级的奖励

        $second_reward =$sys->getValue("order_paike_second_reward");//第二级的奖励

        //通过uid获取两级父id
        $user_tree = Db::name("user_tree")->where("uid",$uid)->find();
        $user_info = Db::name("user")->where("uid",$uid)->find();
        $tree_where["tree_id"] = $user_tree["tree_id"];//根据当前树
        $tree_where["lft"] = array("<",$user_tree["lft"]);//左节点小于
        $tree_where["rgt"] = array(">",$user_tree["rgt"]);//大于右节点              -------------------根据level倒叙选两个
        $parent_ids = Db::name("user_tree")->where("lft","<",$user_tree["lft"])->where("rgt",">",$user_tree["rgt"])->field("uid")->order("level DESC")->limit($level)->select();
        $insert = [];//添加finance数据
        $uids = [];//uid 的集合
        foreach ($parent_ids as $key=>$value){//遍历父id给父id添加金豆

            $finance_cash = 0.00;

            if($key==0){
                $finance_cash = floatval($first_reward);
                $reward_money = $reward_money + $finance_cash;
            }elseif($key==1){
                $finance_cash = floatval($second_reward);
                $reward_money = $reward_money + $finance_cash;
            }
            $uids[] = $value["uid"];
            $insert[$key]["fina_type"] = "in";
            $insert[$key]["obj_type"] = "reward";
            $insert[$key]["obj_id"] = $order_id;
            $insert[$key]["uid"] = $value["uid"];
            $insert[$key]["username"] = $user_tree["username"];
            $insert[$key]["desc"] = "组员订单完成获取的金豆奖励";
            $insert[$key]["order_id"] = $order_id;
            $insert[$key]["fina_cash"] = $finance_cash;
            $insert[$key]["fina_channel"] = 1;
            $insert[$key]["balance"] = ($user_info["balance"]+1);
            $insert[$key]["source"] = "paike";
            $insert[$key]["remark"] = "订单获取的分佣";
            $insert[$key]["on_time"] = time();
        }

       Db::name("user")->where("uid","in",$uids)->setInc("balance",$finance_cash);
       Db::name("finance")->insertAll($insert);

       return $reward_money;

    }


    /**
     * 获取系统奖励
     */
    public function sysReward($task_info,$order_info,$user_info,$reward_money_all){

        $task_id = $task_info["task_id"];//任务id

        $finance = Db::name("finance")->where("source","shop")->where("obj_id",$task_id)->find();//获取商铺的资金流水

        $_money = floatval($finance["fina_cash"])/intval($task_info["total_task_num"]);

        //扣除商家冻结的钱
        Db::name("shop_admin_wallet")->where("admin_id",$task_info["shop_admin_id"])->setDec("frozen_money",$_money);

        $sys_money = floatval($_money)-floatval($reward_money_all);

        $insert_1 = [];
        $insert_1["fina_type"] = "in";
        $insert_1["obj_type"] = "order";
        $insert_1["obj_id"] = $order_info["order_id"];
        $insert_1["uid"] = $order_info["uid"];
        $insert_1["username"] = $user_info["username"];
        $insert_1["desc"] = "订单完成系统获取金豆";
        $insert_1["order_id"] = $order_info["order_id"];
        $insert_1["fina_cash"] = $sys_money;
        $insert_1["fina_channel"] = 1;
        $insert_1["balance"] = 0;
        $insert_1["source"] = "system";
        $insert_1["remark"] = "订单完成获取的金豆";
        $insert_1["on_time"] = time();

        Db::name("finance")->insert($insert_1);//添加财务表




    }


    /**
     *
     * 订单完成扣除金豆
     */
    public function finishedBean($order_info){

        $task_type = $order_info["task_type"];//任务类型

        $user_info = Db::name("user")->where("uid",$order_info["uid"])->find();

        $task_info = Db::name("shop_task")->where("task_id",$order_info["task_id"])->find();

        $paike_money = 0.00;//派克获取的总金额
        //获取配置的参数
        $sys = new \app\admin\model\System();

        switch ($task_type){

            case 1://精刷单


                $order_jindan = $sys->getValue("order_paike_jindan_bean");

                if(!$order_jindan){
                    $order_jindan = 6;//如果没有系统配置默认是6
                }

                $paike_money = $paike_money + floatval($order_jindan);



                break;

            case 2://隔日单


                $order_jindan = $sys->getValue("order_paike_gere_bean");

                if(!$order_jindan){
                    $order_jindan = 9;//如果没有系统配置默认是9
                }

                $paike_money = $paike_money + floatval($order_jindan);

                break;


            case 3://流量单


                $order_jindan = $sys->getValue("order_paike_liuliang_bean");

                if(!$order_jindan){
                    $order_jindan = 1;//如果没有系统配置默认是1
                }

                $paike_money = $paike_money + floatval($order_jindan);

                break;
        }


        if($task_info["is_good_evalute"]==1){//是否好评


            $order_comment = $sys->getValue("order_paike_comment");

            $paike_money = $paike_money + floatval($order_comment);



        }

        if($task_info["is_review"]==1){//是否追评


            $order_comment_review = $sys->getValue("order_paike_comment_review");

            $paike_money = $paike_money + floatval($order_comment_review);



        }

        if($task_info["is_good_evalute"]==1){//是否追评晒图


            $order_comment_review_pic = $sys->getValue("order_paike_comment_review_pic");

            $paike_money = $paike_money + floatval($order_comment_review_pic);



        }

        $insert_1 = [];
        $insert_1["fina_type"] = "in";
        $insert_1["obj_type"] = "order";
        $insert_1["obj_id"] = $order_info["order_id"];
        $insert_1["uid"] = $order_info["uid"];
        $insert_1["username"] = $user_info["username"];
        $insert_1["desc"] = "组员订单完成获取金豆";
        $insert_1["order_id"] = $order_info["order_id"];
        $insert_1["fina_cash"] = $paike_money;
        $insert_1["fina_channel"] = 1;
        $insert_1["balance"] = ($user_info["balance"]+$paike_money);
        $insert_1["source"] = "paike";
        $insert_1["remark"] = "订单完成获取的金豆";
        $insert_1["on_time"] = time();

        Db::name("finance")->insert($insert_1);//添加财务表
        Db::name("user")->where("uid","in",$order_info["uid"])->setInc("balance",$paike_money);//派克余额增加

        $reward_money = 0.00;

        if($task_type!=3) {
            //调用奖励设置
            $reward_money = $this->beanDistribution($order_info["uid"], $order_info["order_id"], 2);//派克奖励
        }


        $reward_money_all = floatval($reward_money)+floatval($paike_money);


        //获取系统奖励
        $this->sysReward($task_info,$order_info,$user_info,$reward_money_all);


    }



}